/***************************************************
> Copyright (C) 2024 ==LENA== All rights reserved.
> File Name: 7.15上1.c
> Author: lena
> Mail:lena_li@126.com
> Created Time: 2024年07月15日 星期一 00时52分33秒
***************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
STU* delete(STU* head);
STU* delay(STU* head);

void outputLink(STU *head);

void main(int argc, const char* argv[])
{
	STU* head = NULL;
	STU* temp = NULL;
	head = createLink();
	head = insertLink(head);
	outputLink(head);
	printf("数据插入链表成功\n");
	head = delete(head);
	printf("删除id为2的链表后：\n");
	outputLink(head);
	head = delay(head);
	printf("把末尾成员name修改后，链表中的信息是：\n");
	outputLink(head);


}
//创建头节点
STU* createLink(void)
{
	STU* head = NULL;
	head = (STU*)malloc(sizeof(STU));
	memset(head, 0, sizeof(STU));
	head->next = NULL;															//头节点之后为空
	printf("初始化成功\n");
	return head;
}
//插入链表
STU* insertLink(STU* head)
{
	assert(head!=NULL);
	//head赋值
	STU* p = head;	
	//尾插法，头不能进去变，所有用指针p进去尾插
	STU* newNode = NULL;
	for (int i = 0; i < 3; i++)
	{
		//创建新节点（这个很重要）
		newNode= (STU*)malloc(sizeof(STU));	
		printf("第%d次\n", i+1);
		printf("请输入id:");
		scanf("%d", &(newNode->id));													//要取地址
		printf("输入姓名:");
		scanf("%s", newNode->name);
									
		
															//重置新节点的后面
		p->next = newNode;	
		newNode->next = NULL;
		//连接节点
		p = newNode;//p = p->next;
		//刷新节点
		
	}
	return head;
}


//删除链表中的成员
STU* delete(STU* head) {
	//删除第二个节点
	assert(head!=NULL);
	STU *bef = head;
	STU *cur = NULL;
	STU *temp = head;
	while(temp!=NULL)
	{
		//找节点
		if(temp->id == 2)
		{
			cur = temp;
			//找到位置
			bef->next = cur->next;
			free(cur);
			cur = NULL;
		}
		bef = temp;
		temp = temp->next;
	}
	return head;
}

//修改链表中的成员数据
STU* delay(STU* head) {
	assert(head!=NULL);
	STU* p = head->next;
	while (p!=NULL)
	{
		if(p->id == 3)
		{
			strcpy(p->name,"dd");
		}
		p = p->next;
	}
	return head;
}
void outputLink(STU *head)
{
	assert(head!=NULL);
	STU *temp = head->next;
	while(temp!=NULL)
	{
		printf("%d %s\n", temp->id, temp->name);
		temp = temp->next;
	}
}

